package com.itheima.controller;

import com.itheima.domain.Assignment;
import com.itheima.domain.User;
import com.itheima.domain.UserStudentAssignment;
import com.itheima.service.AssignmentService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/work")
public class AssignmentController {
    @Autowired
    private AssignmentService assignmentService;

    /**
     * 发布
     */
    @PostMapping("/publishWork")
    public Result publishWork(@RequestBody Assignment assignment) {
        if (assignmentService.publishWork(assignment)) {
            return new Result<Assignment>(0, "操作成功", assignment);
        } else {
            return new Result(1, "操作失败", null);
        }
    }

    @PostMapping("/showWorks")
    public Result selectWork(@RequestBody Assignment assignment) {
        System.out.println(assignment.getTime());
        System.out.println(assignment.getType());
        List<User> userList = assignmentService.selectWork(assignment);
        if (userList == null) {
            return new Result(1, "操作失败", null);
        } else {
            return new Result<>(0, "操作成功", userList);
        }
    }

    @PostMapping("/findNotWorkByTime")
    public Result findNotWorkByTime(@RequestBody UserStudentAssignment userStudentAssignment) {
        List<User> list = assignmentService.selectWorkCondition(userStudentAssignment);
        if (userStudentAssignment == null) {
            return new Result<>(1, "操作失败", null);
        } else {
            return new Result<>(0, "操作成功", list);
        }
    }
}
